package org.yhx.learning.verticle.eventbus;/*
 * Copyright (C), 2024, bigmoon
 * FileName:
 * Author:   bigmoon
 * Date:     2024/9/5 11:08
 * Description: //模块目的、功能描述
 * History: //修改记录
 * &lt;author&gt;      &lt;time&gt;      &lt;version&gt;    &lt;desc&gt;
 * 修改人姓名             修改时间            版本号                  描述
 */

import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.JsonObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Random;
import java.util.UUID;

/**
 * 模拟设备发送温度
 */
public class HeatSenorVerticle extends AbstractVerticle {

    final UUID uuid=UUID.randomUUID();
    final double temp_format=21D;

    private static final Logger LOGGER = LoggerFactory.getLogger(HeatSenorVerticle.class);


    @Override
    public void start() throws Exception {
        EventBus eventBus = vertx.eventBus();
        vertx.setPeriodic(1000,(id)->sendTemp(eventBus));
        sendTemp(eventBus);
    }

    public void sendTemp(EventBus eventBus){
        Random random = new Random();
        JsonObject put = JsonObject.of("uuid", uuid.toString()).put("temp", temp_format + random.nextInt(10));
        eventBus.publish("sensor.updates",put);
        LOGGER.info("消息发送成功:{}",put.encode());
    }
}
